//
//  QDLinesView.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/4/4.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit

/// 线路信息的样式
private final class QDLinesCollectionViewCell: NWCollectionViewCell,RITLSDCycleScrollViewCell {
    
    /// RITLSDCycleScrollViewCell
    var imageView: UIImageView {
        return UIImageView()
    }
    
    /// 背景视图
    let backView = UIView()
    /// 显示的标题标签
    let titleLabel = UILabel()
    
    override func buildView() {
        super.buildView()
        
        backView.clipsToBounds = true
        backView.layer.cornerRadius = NWScale(20)
        
        titleLabel.textAlignment = .center
        titleLabel.textColor = .white
        titleLabel.font = Font.regular.font(size: 16)
        
        contentView.addSubview(backView)
        backView.addSubview(titleLabel)
        
        backView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
}




/// 线路信息
final class QDLinesView: NWPopWindow {

    /// 设置数据，stationId, stationTitle
    var data = [(lineId: String,lineDesc: String)](){
        didSet {
            // 刷新数据
            cycleView.imageURLStringsGroup = Array(repeating: "", count: data.count)
        }
    }
    
    /// 线路点击的回调
    var lineDidTapHandler: ((QDLinesView,Int)->(Void))?
    
    /// 底层的白色装载视图
    private let containerView = UIView()
    /// 关闭按钮
    private let closeButton = UIButton()
    /// 标题
    private let titleLabel = UILabel()
    /// 滚动视图
    private let cycleView = RITLSDCycleScrollView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.black.withAlphaComponent(0.4)

        containerView.backgroundColor = .white
        containerView.layer.cornerRadius = 8
        containerView.frame = CGRect(x: 0, y: ritl_height, width: ritl_width, height: 112 + BarDistance.TabBar.safeDistance/*常用尺寸底部留边*/ + containerView.layer.cornerRadius/*需要加上圆角*/)
        
        closeButton.adjustsImageWhenHighlighted = false
        closeButton.setImage("metro_close".namedImage, for: .normal)
        closeButton.addTarget(self, action: #selector(closeButtonDidTap), for: .touchUpInside)
        
        titleLabel.text = "地铁线路"
        titleLabel.font = Font.regular.font(size: 16)
        titleLabel.textColor = QDColors.black
        
        cycleView.backgroundColor = .white
        cycleView.dataSource = self
        cycleView.delegate = self
        cycleView.infiniteLoop = false
        
        addSubview(containerView)
        containerView.addSubview(closeButton)
        containerView.addSubview(titleLabel)
        containerView.addSubview(cycleView)
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(20)
            make.left.equalToSuperview().offset(20)
        }
        
        closeButton.snp.makeConstraints { (make) in
            make.width.height.equalTo(16)
            make.right.equalToSuperview().inset(20)
            make.top.equalToSuperview().offset(20)
        }
        
        cycleView.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
            make.left.right.equalToSuperview()
            make.height.equalTo(40)
        }
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // MARK: Super
    override func showAnimated() {
        
        UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseIn, animations: {
            self.containerView.ritl_originY = self.ritl_height - self.containerView.ritl_height + self.containerView.layer.cornerRadius
        }, completion: nil)
    }
    
    
    override func dismissAnimated() {
        
        UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {
            self.containerView.ritl_originY = self.ritl_height
        }) { (_) in
            self.removeFromSuperview()
        }
    }
    
    
    // MARK: @objc
    
    /// 关闭按钮点击
    @objc func closeButtonDidTap(){
        dismissAnimated()
    }
}



extension QDLinesView: RITLSDCycleScrollViewDataSource,SDCycleScrollViewDelegate {
    
    func cycleViewCustomCollectionViewCellClass(_ cycleView: RITLSDCycleScrollView) -> AnyClass {
        return QDLinesCollectionViewCell.self
    }
    
    
    func cycleScollViewcustomCollectionViewLayout(_ cycleView: RITLSDCycleScrollView) -> UICollectionViewLayout {
        
        let flowLayout = UICollectionViewFlowLayout()
        
        //设置inset
        flowLayout.scrollDirection = .horizontal
        flowLayout.sectionInset.left = 20
        flowLayout.sectionInset.right = 20
        //大小
        flowLayout.itemSize = CGSize(width: NWScale(70), height: NWScale(40))
        //间距
        flowLayout.minimumLineSpacing = (UIScreen.main.bounds.width - 40 - NWScale(70) * 4) / 3
        
        return flowLayout
    }
    
    
    func cycle(_ cycleView: RITLSDCycleScrollView, setupCustomCell cell: UICollectionViewCell & RITLSDCycleScrollViewCell, forIndex indexPath: IndexPath) {
        
        guard let linesCell = cell as? QDLinesCollectionViewCell else {
            return
        }
        
        //进行赋值
        linesCell.backView.backgroundColor = data[indexPath.item].lineId.metroSelectColor
        linesCell.titleLabel.text = data[indexPath.item].lineDesc
    }
    
    
    
    func cycleScrollView(_ cycleScrollView: SDCycleScrollView!, didSelectItemAt index: Int) {
        //进行回调
        lineDidTapHandler?(self,index)
    }
}
